package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_circle_cate;

import cn.com.api.service.ApiU_circle_cateService;

import cn.com.util.*;

public class U_circle_cateBaseController {

	private static Logger logger = Logger.getLogger(U_circle_cateBaseController.class);

	@Autowired
	private ApiU_circle_cateService apiU_circle_cateService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle_cate-list")
	public ModelAndView u_circle_cate_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_circle_cate_list.jsp");

		List<ApiU_circle_cate> u_circle_cates = apiU_circle_cateService.findU_circle_cates();
		mv.addObject("u_circle_cates", u_circle_cates);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle_cate-add-pre")
	public ModelAndView u_circle_cate_add_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_circle_cate_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_circle_cate u_circle_cate = apiU_circle_cateService.findU_circle_cateById(Integer.valueOf(id));
			mv.addObject("u_circle_cate", u_circle_cate);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle_cate-add")
	public void u_circle_cate_add(HttpServletRequest request, HttpServletResponse response,
			ApiU_circle_cate u_circle_cate) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Integer id = u_circle_cate.getId();
		if (id == null || id == 0) {
			apiU_circle_cateService.insertU_circle_cate(u_circle_cate);
		} else {// edit
			ApiU_circle_cate circlecate = apiU_circle_cateService.findU_circle_cateById(Integer.valueOf(id));
			circlecate.setParent_id(u_circle_cate.getParent_id());
			circlecate.setCate_name(u_circle_cate.getCate_name());
			circlecate.setCate_desc(u_circle_cate.getCate_desc());
			circlecate.setSort(u_circle_cate.getSort());
			circlecate.setType(u_circle_cate.getType());
			apiU_circle_cateService.updateU_circle_cate(u_circle_cate);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_circle_cate-delete")
	public void u_circle_cate_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiU_circle_cateService.deleteU_circle_cate(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
